(function(){"use strict";BX.namespace("BX.Grid");BX.Grid.SettingsWindow=function(t){this.parent=null;this.popupItems=null;this.items=null;this.popup=null;this.sourceContent=null;this.applyButton=null;this.resetButton=null;this.cancelButton=null;this.init(t);BX.onCustomEvent(window,"BX.Grid.SettingsWindow:init",[this])};BX.Grid.SettingsWindow.prototype={init:function(t){this.parent=t;BX.bind(this.parent.getContainer(),"click",BX.proxy(this._onContainerClick,this));BX.addCustomEvent(window,"Grid::columnMoved",BX.proxy(this._onColumnMoved,this))},destroy:function(){BX.unbind(this.parent.getContainer(),"click",BX.proxy(this._onContainerClick,this));BX.removeCustomEvent(window,"Grid::columnMoved",BX.proxy(this._onColumnMoved,this));this.getPopup().close()},getSelectAllButton:function(){if(!this.selectAllButton){this.selectAllButton=BX.Grid.Utils.getByClass(this.getPopup().contentContainer,this.parent.settings.get("classSettingsWindowSelectAll"),true)}return this.selectAllButton},getUnselectAllButton:function(){if(!this.unselectAllButton){this.unselectAllButton=BX.Grid.Utils.getByClass(this.getPopup().contentContainer,this.parent.settings.get("classSettingsWindowUnselectAll"),true)}return this.unselectAllButton},reset:function(){this.popupItems=null;this.allColumns=null;this.items=null},_onContainerClick:function(t){if(BX.hasClass(t.target,this.parent.settings.get("classSettingsButton"))){this._onSettingsButtonClick(t)}},_onSettingsButtonClick:function(){BX.onCustomEvent(window,"BX.Grid.SettingsWindow:show",[this]);this.getPopup().show()},fetchColumns:function(){var t=new BX.Promise;BX.ajax({url:this.parent.getParam("LAZY_LOAD")["GET_LIST"],method:"GET",dataType:"json",onsuccess:t.fulfill.bind(t)});return t},prepareColumnOptions:function(t){var e=this.parent.getUserOptions().getCurrentOptions().custom_names;if(BX.type.isPlainObject(t)){if(BX.type.isPlainObject(e)){if(t.id in e){t.name=e[t.id]}}if(this.parent.getColumnHeaderCellByName(t.id)){t.selected=true}}return t},createColumnElement:function(t){var e='<div data-name="'+t.id+'" class="main-grid-settings-window-list-item">'+'<input id="'+t.id+'-checkbox" type="checkbox" class="main-grid-settings-window-list-item-checkbox"'+(t.selected?" checked":"")+">"+'<label for="'+t.id+'-checkbox" class="main-grid-settings-window-list-item-label">'+t.name+"</label>"+'<span class="main-grid-settings-window-list-item-edit-button"></span>'+"</div>";return BX.create("div",{html:e}).firstElementChild},useLazyLoadColumns:function(){return!!this.parent.getParam("LAZY_LOAD")},getSourceContent:function(){if(!this.sourceContent){this.sourceContent=BX.Grid.Utils.getByClass(this.parent.getContainer(),this.parent.settings.get("classSettingsWindow"),true);if(this.useLazyLoadColumns()){this.contentList=this.sourceContent.querySelector(".main-grid-settings-window-list");this.contentList.innerHTML="";var t=new BX.Loader({target:this.contentList});t.show();this.fetchColumns().then(function(e){e.forEach(function(t){t=this.prepareColumnOptions(t);this.contentList.appendChild(this.createColumnElement(t))},this);t.hide().then(function(){t.destroy()});this.reset();this.getItems().forEach(function(t){BX.bind(t.getNode(),"click",BX.delegate(this.onItemClick,this))},this);this.fixedFooter=BX.create("div",{props:{className:"main-grid-popup-window-buttons-wrapper"},children:[this.sourceContent.querySelector(".popup-window-buttons")]});requestAnimationFrame(function(){this.popup.popupContainer.appendChild(this.fixedFooter);this.fixedFooter.style.width=this.popup.popupContainer.clientWidth+"px"}.bind(this))}.bind(this))}}return this.sourceContent},getPopupItems:function(){var t;if(!this.popupItems){t=this.getPopup().contentContainer;this.popupItems=BX.Grid.Utils.getByClass(t,this.parent.settings.get("classSettingsWindowColumn"))}return this.popupItems},getSelectedColumns:function(){var t=[];this.getItems().forEach(function(e){e.isSelected()&&t.push(e.getId())});return t},restoreColumns:function(){this.getItems().forEach(function(t){t.restore()});this.sortItems();this.reset()},restoreLastColumns:function(){this.getItems().forEach(function(t){t.restoreState()})},updateColumnsState:function(){this.getItems().forEach(function(t){t.updateState()})},getStickedColumns:function(){return this.getItems().reduce(function(t,e){if(e.isSticked()){t.push(e.getId())}return t},[])},saveColumns:function(t,e){var n=this.parent.getUserOptions();var i=this.getColumnNames();var s=this.getStickedColumns();var o=[];o.push({action:n.getAction("GRID_SET_COLUMNS"),columns:t.join(",")});o.push({action:n.getAction("SET_CUSTOM_NAMES"),custom_names:i});o.push({action:n.getAction("GRID_SET_STICKED_COLUMNS"),stickedColumns:s});if(this.isForAll()){o.push({action:n.getAction("GRID_SAVE_SETTINGS"),view_id:"default",set_default_settings:"Y",delete_user_settings:"Y"})}n.batch(o,BX.delegate(function(){this.parent.reloadTable(null,null,e)},this));this.updateColumnsState()},disableAllColumnslabelEdit:function(){this.getItems().forEach(function(t){t.disableEdit()})},getAllColumns:function(){if(!this.allColumns){this.allColumns=this.getItems().map(function(t){return t.getId()})}return this.allColumns},isShowedColumn:function(t){return this.getSelectedColumns().some(function(e){return e===t})},getShowedColumns:function(){var t=[];var e=this.parent.getRows().getHeadFirstChild().getCells();[].slice.call(e).forEach(function(e){if("name"in e.dataset){t.push(e.dataset.name)}});return t},sortItems:function(){var t=this.getShowedColumns();var e={};this.getAllColumns().forEach(function(t){e[t]=t},this);var n=0;Object.keys(e).forEach(function(i){if(this.isShowedColumn(i)){e[i]=t[n];n++}var s=this.getColumnByName(e[i]);s&&s.parentNode.appendChild(s)},this)},getColumnNames:function(){var t={};this.getItems().map(function(e){if(e.isEdited()){t[e.getId()]=e.getTitle()}});return t},getColumnByName:function(t){return BX.Grid.Utils.getBySelector(this.getPopup().popupContainer,"."+this.parent.settings.get("classSettingsWindowColumn")+'[data-name="'+t+'"]',true)},_onColumnMoved:function(){this.sortItems();this.reset()},onResetButtonClick:function(){this.parent.confirmDialog({CONFIRM:true,CONFIRM_MESSAGE:this.parent.arParams.CONFIRM_RESET_MESSAGE},BX.delegate(function(){this.enableWait(this.getApplyButton());this.parent.getUserOptions().reset(this.isForAll(),BX.delegate(function(){this.parent.reloadTable(null,null,BX.delegate(function(){this.restoreColumns();this.disableWait(this.getApplyButton());this.getPopup().close()},this))},this))},this))},getResetButtonId:function(){return this.parent.getContainerId()+"-grid-settings-reset-button"},onApplyButtonClick:function(){this.parent.confirmDialog({CONFIRM:this.isForAll(),CONFIRM_MESSAGE:this.parent.getParam("SETTINGS_FOR_ALL_CONFIRM_MESSAGE")},BX.delegate(function(){this.enableWait(this.getApplyButton());this.saveColumns(this.getSelectedColumns(),BX.delegate(function(){this.getPopup().close();this.disableWait(this.getApplyButton());this.unselectForAllCheckbox()},this));BX.onCustomEvent(window,"BX.Grid.SettingsWindow:save",[this])},this),BX.delegate(function(){this.unselectForAllCheckbox()},this))},getApplyButtonId:function(){return this.parent.getContainerId()+"-grid-settings-apply-button"},getApplyButton:function(){if(this.applyButton===null){this.applyButton=BX(this.getApplyButtonId())}return this.applyButton},getCancelButtonId:function(){return this.parent.getContainerId()+"-grid-settings-cancel-button"},getCancelButton:function(){if(this.cancelButton===null){this.cancelButton=BX(this.getCancelButtonId())}return this.cancelButton},unselectForAllCheckbox:function(){var t=this.getForAllCheckbox();t&&(t.checked=null)},enableWait:function(t){BX.addClass(t,"webform-small-button-wait");BX.removeClass(t,"popup-window-button")},disableWait:function(t){BX.removeClass(t,"webform-small-button-wait");BX.addClass(t,"popup-window-button")},createTitle:function(){var t=BX.create("div");var e=BX("pagetitle");var n=!!e?"&laquo;"+e.innerText+"&raquo;":"";t.innerHTML="<span>"+this.parent.getParam("SETTINGS_TITLE")+" "+n+"</span>";return t.firstChild.innerText},getPopupId:function(){return this.parent.getContainerId()+"-grid-settings-window"},createPopup:function(){if(!this.popup){this.popup=new BX.PopupWindow(this.getPopupId(),null,{titleBar:this.createTitle(),autoHide:false,overlay:.6,width:1e3,closeIcon:true,closeByEsc:true,contentNoPaddings:true,content:this.getSourceContent(),events:{onPopupClose:BX.delegate(this.onPopupClose,this)}});this.getItems().forEach(function(t){BX.bind(t.getNode(),"click",BX.delegate(this.onItemClick,this))},this);BX.bind(this.getResetButton(),"click",BX.proxy(this.onResetButtonClick,this));BX.bind(this.getApplyButton(),"click",BX.proxy(this.onApplyButtonClick,this));BX.bind(this.getCancelButton(),"click",BX.proxy(this.popup.close,this.popup));BX.bind(this.getSelectAllButton(),"click",BX.delegate(this.onSelectAll,this));BX.bind(this.getUnselectAllButton(),"click",BX.delegate(this.onUnselectAll,this))}return this.popup},onItemClick:function(){this.adjustActionButtonsState()},getItems:function(){if(this.items===null){this.items=this.getPopupItems().map(function(t){return new BX.Grid.SettingsWindowColumn(this.parent,t)},this)}return this.items},onPopupClose:function(){BX.onCustomEvent(window,"BX.Grid.SettingsWindow:close",[this]);this.restoreLastColumns();this.disableAllColumnslabelEdit();this.adjustActionButtonsState()},getPopup:function(){return!!this.popup?this.popup:this.popup=this.createPopup()},onSelectAll:function(){this.selectAll();this.enableActions()},onUnselectAll:function(){this.unselectAll();this.disableActions()},selectAll:function(){this.getItems().forEach(function(t){t.select()})},unselectAll:function(){this.getItems().forEach(function(t){t.unselect()})},isForAll:function(){var t=this.getForAllCheckbox();return t&&!!t.checked},getForAllCheckbox:function(){return BX.Grid.Utils.getByClass(this.getPopup().popupContainer,"main-grid-settings-window-for-all-checkbox",true)},getResetButton:function(){if(this.resetButton===null){this.resetButton=BX(this.getResetButtonId())}return this.resetButton},disableActions:function(){var t=this.getApplyButton();if(!!t){BX.addClass(t,this.parent.settings.get("classDisable"))}},enableActions:function(){var t=this.getApplyButton();if(!!t){BX.removeClass(t,this.parent.settings.get("classDisable"))}},adjustActionButtonsState:function(){if(this.getSelectedColumns().length){this.enableActions()}else{this.disableActions()}}}})();